

set more off
mat drop _all


use "$path_data/data/temp/main_reg_data_psc.dta", clear
foreach i in 10 20 30 40 50 60 70 80 90 {
egen p_`i' = pctile(ptsbl_overall_score), p(`i') 
}
save "$path_data/data/temp/main_reg_data_psc_hetero.dta", replace

* 0-10 Quantile
qui teffects ipw (math_score_std) (treatment student_gender age age_sq, probit) if ptsbl_overall_score != . & ptsbl_overall_score < p_20, atet
parmest, label list(parm label estimate min* max* p) saving(results_1, replace)
use results_1, clear
replace parm = "0-20%" in 2
keep in 2
save "$path_data/data/temp/results_1.dta", replace
clear




* 10-20 Quantile
use "$path_data/data/temp/main_reg_data_psc_hetero.dta", clear

qui teffects ipw (math_score_std) (treatment student_gender age age_sq, probit) if ptsbl_overall_score != . & p_20 <= ptsbl_overall_score & ptsbl_overall_score < p_40, atet
parmest, label list(parm label estimate min* max* p) saving(results_2, replace)
use results_2, clear
replace parm = "20-40%" in 2
keep in 2
save "$path_data/data/temp/results_2.dta", replace
clear



* 20-30 Quantile
use "$path_data/data/temp/main_reg_data_psc_hetero.dta", clear

qui teffects ipw (math_score_std) (treatment student_gender age age_sq, probit) if ptsbl_overall_score != . & p_40 <= ptsbl_overall_score & ptsbl_overall_score < p_60, atet
parmest, label list(parm label estimate min* max* p) saving(results_3, replace)
use results_3, clear
replace parm = "40-60%" in 2
keep in 2
save "$path_data/data/temp/results_3.dta", replace
clear




* 30-40 Quantile
use "$path_data/data/temp/main_reg_data_psc_hetero.dta", clear

qui teffects ipw (math_score_std) (treatment student_gender age age_sq, probit) if ptsbl_overall_score != . & p_60 <= ptsbl_overall_score & ptsbl_overall_score < p_80, atet
parmest, label list(parm label estimate min* max* p) saving(results_4, replace)
use results_4, clear
replace parm = "60-80%" in 2
keep in 2
save "$path_data/data/temp/results_4.dta", replace
clear




* 40-50 Quantile
use "$path_data/data/temp/main_reg_data_psc_hetero.dta", clear

qui teffects ipw (math_score_std) (treatment student_gender age age_sq, probit) if ptsbl_overall_score != . & p_80 <= ptsbl_overall_score , atet
parmest, label list(parm label estimate min* max* p) saving(results_5, replace)
use results_5, clear
replace parm = "80-100%" in 2
keep in 2
save "$path_data/data/temp/results_5.dta", replace
clear






use "$path_data/data/temp/results_1.dta"
forvalues i = 2/5 {
append using "$path_data/data/temp/results_`i'.dta"
}
forvalues i = 1/5 {
rm "$path_data/data/temp/results_`i'.dta"
}
 


encode parm, generate(parm2)
drop parm
rename parm2 parm
label variable estimate "regression coefficient"
label variable min95 "lower 95% CI "
label variable max95 " upper 95% CI"
twoway (scatter estimate parm, mcolor(black) msymbol(square) msize(small)) (rcap min95 max95 parm, lcolor(black)) (line estimate parm, lcolor(black)), xlabel(, valuelabels angle(0) labsize(small)) ylabel(, angle(3)) xtitle("Quantile of Baseline PTS-II Score") ytitle("Treatment Effects") legend(order(1 "Point Estimation" 2 "Upper/Lower 95% CI")) graphregion(color(white))
graph export "$pardir/figureH1.pdf", replace as(pdf)
















